<template>
  <div class="dinnerRegister">
    <cus-card title="单日伙食登记管理">
      <Form ref="formValidate" :model="form" label-position="left">
        <div style="display: flex">
          <FormItem
            :class="
             'cookTable-item'
            "
            label="登记日期:"
            prop="huoshidate"
            class="mr_20"
            :rules="{
              required: true,
              type: 'date',
              message: '登记日期不能为空',
              trigger: 'change'
            }"
          >
            <DatePicker
              type="date"
              placeholder="登记日期"
              v-if="!huoshiid"
              transfer
              clearabled
              v-model="form.huoshidate"
              @on-change="onDateChange"
            />
            <div
              v-else-if="form.huoshidate"
              style="width: 200px;display: flex;padding-left: 20px"
            >
              {{ form.huoshidate.Format("yyyy-MM-dd") }}
            </div>
          </FormItem>
          <FormItem
            :class="
          'cookTable-item ml_20'
            "
            class="mr_20"
            label="选择食谱:"
            prop="mealPlanId"
            :rules="{
              required: true,
              type: 'number',
              message: '食谱不能为空',
              trigger: 'change'
            }"
          >
            <Select
              v-if="!huoshiid"
              v-model="form.mealPlanId"
              clearable
              transfer
              placeholder="选择食谱"
              style="width: 200px;"
              label-in-value
              @on-change="onMealChange"
            >
              <Option
                :value="v.mealPlanId"
                v-for="(v, i) in mealPlanList"
                :key="i"
                >{{ v.name }}</Option
              >
            </Select>
            <div v-else style="width: 200px;display: flex;padding-left: 20px">
              {{ form.mealPlanName }}
            </div>
          </FormItem>
          <FormItem
            :class="
            'cookTable-item ml_20'
            "
            class="mr_20"
            label="登记人:"
            prop="remark"
            :rules="{
              required: true,
              type: 'string',
              message: '登记人不能为空',
              trigger: 'blur'
            }"
          >
            <Input
              type="text"
              v-if="!huoshiid"
              v-model="form.remark"
              maxlength="30"
              clearable
              placeholder="登记人"
            ></Input>
            <div v-else style="width: 200px;display: flex;padding-left: 20px">
              {{ form.remark }}
            </div>
          </FormItem>
        </div>
        <Table border :columns="columns1" :data="form.foodarray">
          <template slot-scope="{ row, index }" slot="can">
            {{ row.can == 0 ? "早餐" : "" }}
            {{ row.can == 1 ? "午餐" : "" }}
            {{ row.can == 2 ? "晚餐" : "" }}
          </template>
          <template slot-scope="{ row, index }" slot="foodname">
            <div style="display: flex;align-items: center;flex-wrap: wrap;">
              <Tag
                color="success"
                v-for="(v, i) in form['foodarray'][index].foodname"
                :key="i"
                >{{ v.dishName }}</Tag
              >
              <!-- <div style="display: flex" v-if="!huoshiid">
                <cus-button
                  type="text"
                  size="small"
                  :style="{ color: '#1E6FFF', padding: '0 8px' }"
                  @onClick="onAddFood(index)"
                >
                  新增
                </cus-button>
                <cus-button
                  type="text"
                  size="small"
                  :style="{ color: '#FF4746', padding: '0 8px' }"
                  @onClick="onClear(index)"
                >
                  清空
                </cus-button>
              </div> -->
            </div>
          </template>
          <template slot-scope="{ row, index }" slot="whole">
            {{ form.foodarray[index].whole }}
          </template>
          <template slot-scope="{ row, index }" slot="liandui">
            <InputNumber
              v-if="!huoshiid"
              style="width: 100%;"
              size="large"
              :precision="0"
              :min="0"
              :max="99999"
              v-model="form.foodarray[index].liandui"
              placeholder="连队人数"
              @on-change="onPersonChange(index)"
            ></InputNumber>
            <div v-else>{{ form.foodarray[index].liandui }}</div>
          </template>
          <template slot-scope="{ row, index }" slot="wailai">
            <InputNumber
              v-if="!huoshiid"
              style="width: 100%;"
              size="large"
              :precision="0"
              :min="0"
              :max="99999"
              v-model="form.foodarray[index].wailai"
              placeholder="外来人数"
              @on-change="onPersonChange(index)"
            ></InputNumber>
            <div v-else>{{ form.foodarray[index].wailai }}</div>
          </template>
        </Table>

        <cus-card title="单日消耗">
          <Table border :columns="columns2" :data="form.canhaoarray">
            <template slot-scope="{ row, index }" slot="can">
              <!--
              {{ row.can == 0 ? "早餐(千克)" : "" }}
              {{ row.can == 1 ? "午餐(千克)" : "" }}
              {{ row.can == 2 ? "晚餐(千克)" : "" }}
              -->
              {{ row.can == 3 ? "合计(人)" : "" }}
              {{ row.can == 4 ? "人均(克)" : "" }}
            </template>

            <template slot-scope="{ row, index }" slot="columns0">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns0"
                placeholder="请输入"
                @on-change="() => onWeightChange(0)"
              ></InputNumber>
              <div v-else>{{ row.columns0 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns1">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns1"
                placeholder="请输入"
                @on-change="() => onWeightChange(1)"
              ></InputNumber>
              <div v-else>{{ row.columns1 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns2">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns2"
                placeholder="请输入"
                @on-change="() => onWeightChange(2)"
              ></InputNumber>
              <div v-else>{{ row.columns2 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns3">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns3"
                placeholder="请输入"
                @on-change="() => onWeightChange(3)"
              ></InputNumber>
              <div v-else>{{ row.columns3 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns4">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns4"
                placeholder="请输入"
                @on-change="() => onWeightChange(4)"
              ></InputNumber>
              <div v-else>{{ row.columns4 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns5">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns5"
                placeholder="请输入"
                @on-change="() => onWeightChange(5)"
              ></InputNumber>
              <div v-else>{{ row.columns5 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns6">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns6"
                placeholder="请输入"
                @on-change="() => onWeightChange(6)"
              ></InputNumber>
              <div v-else>{{ row.columns6 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns7">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns7"
                placeholder="请输入"
                @on-change="() => onWeightChange(7)"
              ></InputNumber>
              <div v-else>{{ row.columns7 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns8">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns8"
                placeholder="请输入"
                @on-change="() => onWeightChange(8)"
              ></InputNumber>
              <div v-else>{{ row.columns8 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns9">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns9"
                placeholder="请输入"
                @on-change="() => onWeightChange(9)"
              ></InputNumber>
              <div v-else>{{ row.columns9 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns10">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns10"
                placeholder="请输入"
                @on-change="() => onWeightChange(10)"
              ></InputNumber>
              <div v-else>{{ row.columns10 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns11">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns11"
                placeholder="请输入"
                @on-change="() => onWeightChange(11)"
              ></InputNumber>
              <div v-else>{{ row.columns11 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns12">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns12"
                placeholder="请输入"
                @on-change="() => onWeightChange(12)"
              ></InputNumber>
              <div v-else>{{ row.columns12 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns13">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns13"
                placeholder="请输入"
                @on-change="() => onWeightChange(13)"
              ></InputNumber>
              <div v-else>{{ row.columns13 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns14">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns14"
                placeholder="请输入"
                @on-change="() => onWeightChange(14)"
              ></InputNumber>
              <div v-else>{{ row.columns14 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns15">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns15"
                placeholder="请输入"
                @on-change="() => onWeightChange(15)"
              ></InputNumber>
              <div v-else>{{ row.columns15 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns16">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns16"
                placeholder="请输入"
                @on-change="() => onWeightChange(16)"
              ></InputNumber>
              <div v-else>{{ row.columns16 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns17">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns17"
                placeholder="请输入"
                @on-change="() => onWeightChange(17)"
              ></InputNumber>
              <div v-else>{{ row.columns17 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns18">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns18"
                placeholder="请输入"
                @on-change="() => onWeightChange(18)"
              ></InputNumber>
              <div v-else>{{ row.columns18 }}</div>
            </template>
            <template slot-scope="{ row, index }" slot="columns19">
              <InputNumber
                style="width: 100%;"
                size="large"
                v-if="row.can != 3 && row.can != 4 && !huoshiid"
                :min="0"
                :max="99999"
                v-model="form.canhaoarray[index].columns19"
                placeholder="请输入"
                @on-change="() => onWeightChange(19)"
              ></InputNumber>
              <div v-else>{{ row.columns19 }}</div>
            </template>
          </Table>
        </cus-card>
        <cus-card title="实物验收">
          <Table
            border
            :columns="columns3"
            :data="form.rukuarray"
            :loading="form.loading3"
            max-height="500"
          >
            <template slot-scope="{ row, index }" slot="packagingPrice">
              {{ row.packagingPrice }}
              <!-- {{ ((row.price ? row.price : 0) / 1000).toFixed(2) }} -->
            </template>
            <template slot-scope="{ row, index }" slot="packagingQty">
              {{ row.packagingQty }}
              <!-- {{ ((row.weight ? row.weight : 0) / 1000).toFixed(2) }} -->
            </template>
            <template slot-scope="{ row, index }" slot="amount">
              {{ row.amount }}
              <!-- {{ ((row.amount ? row.amount : 0) / 1000000).toFixed(2) }} -->
            </template>
            <template slot-scope="{ row, index }" slot="isProduced">
              <Tag v-if="row.isProduced == 1" color="green">自产</Tag>
              <Tag color="red" v-else>购买</Tag>
            </template>
          </Table>
        </cus-card>
        <div class="button-textarea-wrap">
          <FormItem class="cookTable-item" label="炊事员及食堂卫生情况">
            <Input
              v-if="!huoshiid"
              type="textarea"
              v-model="form.weisheng"
              maxlength="100"
              :rows="5"
              clearable
              placeholder="炊事员及食堂卫生情况"
            ></Input>
            <div v-else style="width: 200px;display: flex;padding-left: 20px">
              {{ form.weisheng }}
            </div>
          </FormItem>
          <FormItem class="cookTable-item ml_20" label="帮厨情况">
            <Input
              v-if="!huoshiid"
              type="textarea"
              v-model="form.yaoshi"
              maxlength="100"
              :rows="5"
              clearable
              placeholder="帮厨情况"
            ></Input>
            <div v-else style="width: 200px;display: flex;padding-left: 20px">
              {{ form.yaoshi }}
            </div>
          </FormItem>
          <FormItem class="cookTable-item ml_20" label="就餐人员意见">
            <Input
              v-if="!huoshiid"
              type="textarea"
              v-model="form.yijian"
              maxlength="100"
              :rows="5"
              clearable
              placeholder="就餐人员意见"
            ></Input>
            <div v-else style="width: 200px;display: flex;padding-left: 20px">
              {{ form.yijian }}
            </div>
          </FormItem>
        </div>
        <div class="button-textarea-wrap">
          <FormItem class="cookTable-item" label="单位首长">
            <Input
              v-if="!huoshiid"
              type="textarea"
              v-model="form.head"
              maxlength="100"
              :rows="5"
              clearable
              placeholder="单位首长"
            ></Input>
            <div v-else style="width: 200px;display: flex;padding-left: 20px">
              {{ form.head }}
            </div>
          </FormItem>
          <FormItem class="cookTable-item ml_20" label="经委会主任">
            <Input
              v-if="!huoshiid"
              type="textarea"
              v-model="form.director"
              maxlength="100"
              :rows="5"
              clearable
              placeholder="经委会主任"
            ></Input>
            <div v-else style="width: 200px;display: flex;padding-left: 20px">
              {{ form.director }}
            </div>
          </FormItem>
          <FormItem class="cookTable-item ml_20" label="厨房值班员(签字)">
            <Input
              v-if="!huoshiid"
              type="textarea"
              v-model="form.zhibanyuan"
              maxlength="100"
              :rows="5"
              clearable
              placeholder="厨房值班员"
            ></Input>
            <div v-else style="width: 200px;display: flex;padding-left: 20px">
              {{ form.zhibanyuan }}
            </div>
          </FormItem>
        </div>
      </Form>
    </cus-card>
    <div style="display: flex;justify-content: flex-end; background: #ffffff;" v-if="!huoshiid">
      <cusButton class="mr_20 mb_20 mt_10" type="primary" @onClick="onOk"
        >&nbsp;&nbsp;保存&nbsp;&nbsp;</cusButton
      >
    </div>
    <Spin fix v-if="form.loading"></Spin>
    <chooseMealModal ref="chooseMealModal"></chooseMealModal>
  </div>
</template>

<script>
import {
  getThatDayStockBillCheckList,
  addDinnerRegister,
  getPartnerList,
  getDepartment,
  getDinnerRegisterInfo,
  getMealMsg
} from "@/api/business/business";
import {
  getMealPlanList,
  getFoodConsumption
} from "@/api/business/dishesModule";
import chooseMealModal from "./components/chooseMealModal/chooseMealModal";

const form = {
  huoshidate: "",
  mealPlanId: "",
  mealPlanName: "",
  foodarray: [
    { foodname: [], can: 0, whole: 0, liandui: 0, wailai: 0 },
    { foodname: [], can: 1, whole: 0, liandui: 0, wailai: 0 },
    { foodname: [], can: 2, whole: 0, liandui: 0, wailai: 0 }
  ], // 餐次菜肴
  canhaoarray: [
    {
      can: 3,
      columns0: 0,
      columns1: 0,
      columns2: 0,
      columns3: 0,
      columns4: 0,
      columns5: 0,
      columns6: 0,
      columns7: 0,
      columns8: 0,
      columns9: 0,
      columns10: 0,
      columns11: 0,
      columns12: 0,
      columns13: 0,
      columns14: 0,
      columns15: 0,
      columns16: 0,
      columns17: 0,
      columns18: 0,
      columns19: 0
    },
    {
      can: 4,
      columns0: 0,
      columns1: 0,
      columns2: 0,
      columns3: 0,
      columns4: 0,
      columns5: 0,
      columns6: 0,
      columns7: 0,
      columns8: 0,
      columns9: 0,
      columns10: 0,
      columns11: 0,
      columns12: 0,
      columns13: 0,
      columns14: 0,
      columns15: 0,
      columns16: 0,
      columns17: 0,
      columns18: 0,
      columns19: 0
    }
  ],
  rukuarray: [], // 物料验收
  remark: "",
  weisheng: "",
  yijian: "",
  yaoshi: "",
  head: "",
  director: "",
  zhibanyuan: "",
  loading3: false,
  loading: false
};

export default {
  name: "dinnerRegister",
  components: { chooseMealModal },
  computed: {
    personTotal() {
      let total = 0;
      this.form.foodarray.forEach(v => {
        total += v.whole ? v.whole : 0;
      });
      return total;
    }
  },
  data() {
    return {
      huoshiid: "",
      form: form,
      columns1: [
        {
          title: " ",
          align: "center",
          width: "70",
          slot: "can"
        },
        {
          title: " ",
          slot: "foodname"
        },
        {
          title: "总人数",
          align: "center",
          width: "110",
          slot: "whole"
        },
        {
          title: "连队人数",
          align: "center",
          width: "110",
          slot: "liandui"
        },
        {
          title: "外来人数",
          align: "center",
          width: "110",
          slot: "wailai"
        }
      ],
      columns2: [
        {
          title: " ",
          align: "center",
          width: "120",
          slot: "can"
        },
        {
          title: "粮食",
          align: "center",
          slot: "columns0",
          minWidth: 100
        },
        {
          title: "蓄肉",
          align: "center",
          slot: "columns1",
          minWidth: 100
        },
        {
          title: "禽肉",
          align: "center",
          slot: "columns2",
          minWidth: 100
        },
        {
          title: "禽蛋",
          align: "center",
          slot: "columns3",
          minWidth: 100
        },
        {
          title: "鱼虾类",
          align: "center",
          slot: "columns4",
          minWidth: 100
        },
        {
          title: "牛奶",
          align: "center",
          slot: "columns5",
          minWidth: 100
        },
        {
          title: "大豆",
          align: "center",
          slot: "columns6",
          minWidth: 100
        },
        {
          title: "蔗糖",
          align: "center",
          slot: "columns7",
          minWidth: 100
        },
        {
          title: "植物油",
          align: "center",
          slot: "columns8",
          minWidth: 100
        },
        {
          title: "蔬菜",
          align: "center",
          slot: "columns9",
          minWidth: 100
        },
        {
          title: "水果",
          align: "center",
          slot: "columns10",
          minWidth: 100
        },
        {
          title: "食用菌",
          align: "center",
          slot: "columns11",
          minWidth: 100
        },
        {
          title: "干菜类",
          align: "center",
          slot: "columns12",
          minWidth: 100
        },
        {
          title: "巧克力",
          align: "center",
          slot: "columns13",
          minWidth: 100
        },
        {
          title: "复合维生素",
          align: "center",
          slot: "columns14",
          minWidth: 100
        },
        {
          title: "饮料",
          align: "center",
          slot: "columns15",
          minWidth: 100
        },
        {
          title: "调料",
          align: "center",
          slot: "columns16",
          minWidth: 100
        },
        {
          title: "罐头类",
          align: "center",
          slot: "columns17",
          minWidth: 100
        },
        {
          title: "零食饼干",
          align: "center",
          slot: "columns18",
          minWidth: 100
        },
        {
          title: "其他",
          align: "center",
          slot: "columns19",
          minWidth: 100
        }
      ],
      columns3: [
        {
          width: 70,
          title: "序号",
          key: "name",
          type: "index",
          align: "center"
        },
        {
          title: "品名",
          minWidth: 150,
          key: "productName",
          align: "center"
        },
        {
          title: "价格",
          minWidth: 150,
          slot: "packagingPrice",
          align: "center"
        },
        {
          title: "数量",
          minWidth: 150,
          slot: "packagingQty",
          align: "center"
        },
        {
          title: "金额",
          minWidth: 150,
          slot: "amount",
          align: "center"
        },
        {
          title: "购买或自产",
          width: 150,
          slot: "isProduced",
          align: "center"
        }
      ],
      mealPlanList: [],
      departmentList: [] // 单位列表
    };
  },
  watch: {
    huoshiid: {
      handler(val) {
        this.form = this.$cu.deepClone(form);
        this.getDepartmentApi();
        if (this.huoshiid) {
          this.form.loading = true;
          this.getDinnerRegisterInfoApi();
        }
      }
    }
  },
  mounted() {
    this.huoshiid = this.$route.query.id;
    this.form = this.$cu.deepClone(form);
    this.getDepartmentApi();
    if (this.huoshiid) {
      this.form.loading = true;
      this.getDinnerRegisterInfoApi();
    }
  },
  methods: {
    getFoodConsumption(date) {
      let params = {};
      params.date = date;
      let foodConsumptionOption = {};
      getFoodConsumption(params).then(res => {
        if (res.retCode == 2000) {
          foodConsumptionOption = res.data;
          console.log(this.form.canhaoarray);
          this.columns2.forEach((item, index) => {
            if (item.slot != "can") {
              this.form.canhaoarray[0][item.slot] = res.data[item.title]
                ? res.data[item.title]
                : 0;
            }
          });
        }
        this.calcPerAverage();
        console.log(this.form.canhaoarray);
      });
    },
    // 获取食谱的菜肴详情
    getMealMsg(params) {
      getMealMsg(params).then(res => {
        if (res.retCode == 2000) {
          this.form.foodarray[0].foodname = res.data.morning;
          this.form.foodarray[1].foodname = res.data.centre;
          this.form.foodarray[2].foodname = res.data.lateness;
        }
      });
    },
    getMealPlanList(data) {
      let params = {};
      params.date = data;
      getMealPlanList(params).then(res => {
        if (res.retCode == 2000) {
          this.mealPlanList = res.data;
        }
      });
    },
    // 登记日期切换
    onDateChange(date) {
      if (date) {
        this.$nextTick(() => {
          this.getThatDayStockBillCheckListApi();
          this.getMealPlanList(date);
          this.getFoodConsumption(date);
        });
      }
    },
    // 食谱选中事件
    onMealChange(v) {
      let params = {};
      params.date = new Date(this.form.huoshidate).Format("yyyy-MM-dd");
      params.mealPlanId = this.form.mealPlanId;
      if (v) {
        this.form.mealPlanName = v.label;
        this.getMealMsg(params);
      }
    },
    // 单日消耗数据切换
    onWeightChange(index) {
      let sum = 0;
      this.form.canhaoarray.forEach(v => {
        if (v.can == 0 || v.can == 1 || v.can == 2) {
          sum += v[`columns${index}`] ? v[`columns${index}`] : 0;
        }
      });
      // 合计计算
      this.form.canhaoarray[3][`columns${index}`] = sum.toFixed(2);
      // 计算人均
      this.calcPerAverage();
    },
    // 新增菜肴
    onAddFood() {
      if (!this.form.huoshidate || !this.form.mealPlanId)
        return this.$Message.error("请先选择日期与食谱");
      this.$refs["chooseMealModal"].form.date = this.form.huoshidate.Format(
        "yyyy-MM-dd"
      );
      this.$refs["chooseMealModal"].form.mealPlanId = this.form.mealPlanId;
      this.$refs[
        "chooseMealModal"
      ].form.chooseList.morning = this.form.foodarray[0].foodname;
      this.$refs[
        "chooseMealModal"
      ].form.chooseList.centre = this.form.foodarray[1].foodname;
      this.$refs[
        "chooseMealModal"
      ].form.chooseList.lateness = this.form.foodarray[2].foodname;
      this.$refs["chooseMealModal"].show();
    },
    // 清空食谱
    onClear(index) {
      this.form.foodarray[index]["foodname"] = [];
    },
    // 保存按钮
    onOk() {
      this.$refs[`formValidate`].validate(valid => {
        if (valid) {
          this.$cu
            .dialog({ content: "确认保存该数据?", title: "提示" })
            .then(() => {
              this.form.loading = true;
              this.addDinnerRegisterApi();
            });
        }
      });
    },
    // 人数切换
    onPersonChange(index) {
      let liandui = this.form.foodarray[index]["liandui"]
        ? this.form.foodarray[index]["liandui"]
        : 0;
      let wailai = this.form.foodarray[index]["wailai"]
        ? this.form.foodarray[index]["wailai"]
        : 0;
      this.form.foodarray[index]["whole"] = liandui + wailai;
      // 计算人均
      this.calcPerAverage();
    },
    // 计算人均
    calcPerAverage() {
      // 计算人均
      for (const key in this.form.canhaoarray[0]) {
        if (key.includes("columns") && this.personTotal) {
          this.form.canhaoarray[1][key] = (
            this.form.canhaoarray[0][key] / this.personTotal
          ).toFixed(2);
        }
      }
    },
    // 获取实物验收数据
    getThatDayStockBillCheckListApi() {
      this.form.loading3 = true;
      return new Promise(resolve => {
        getThatDayStockBillCheckList({
          date: this.form.huoshidate.Format("yyyy-MM-dd")
        }).then(res => {
          this.form.loading3 = false;
          if (res.retCode == 2000) {
            this.form.rukuarray = res.data;
          }
        });
      });
    },
    // 获取单位数据
    getDepartmentApi() {
      return new Promise(resolve => {
        getDepartment({}).then(res => {
          if (res.retCode === 2000) {
            this.departmentList = res.data;
          }
        });
      });
    },
    // 获取单日伙食管理详情
    getDinnerRegisterInfoApi() {
      return new Promise(resolve => {
        getDinnerRegisterInfo({ huoshiid: this.huoshiid }).then(res => {
          this.form.loading = false;
          console.log(res);
          this.form.foodarray[0]["liandui"] = res.huoshi.lianduib;
          this.form.foodarray[1]["liandui"] = res.huoshi.lianduil;
          this.form.foodarray[2]["liandui"] = res.huoshi.lianduis;
          this.form.foodarray[0]["wailai"] = res.huoshi.wailaib;
          this.form.foodarray[1]["wailai"] = res.huoshi.wailail;
          this.form.foodarray[2]["wailai"] = res.huoshi.wailais;
          this.form.foodarray[0]["whole"] = res.huoshi.wholeb;
          this.form.foodarray[1]["whole"] = res.huoshi.wholel;
          this.form.foodarray[2]["whole"] = res.huoshi.wholes;

          this.form.mealPlanId = res.huoshi.mealPlanId;
          this.form.mealPlanName = res.huoshi.mealPlanName;

          this.form.huoshidate = new Date(res.huoshi.huoshiDate);
          console.log(this.form.huoshidate);
          this.form.remark = res.huoshi.remark;
          this.form.weisheng = res.huoshi.weisheng;
          this.form.yijian = res.huoshi.yijian;
          this.form.yaoshi = res.huoshi.yaoshi;
          this.form.head = res.huoshi.head;
          this.form.director = res.huoshi.director;
          this.form.zhibanyuan = res.huoshi.zhibanyuan;
          this.form.foodarray.forEach((item) => {
            item.foodname = [];
          });
          console.log('this.form.foodarray设备',this.form.foodarray);
          // this.form.foodarray[0].foodname = [];
          // this.form.foodarray[1].foodname = [];
          // this.form.foodarray[2].foodname = [];
          res.food.forEach(v => {
            v.dishName = v.foodname;
            this.form.foodarray[v.can]["foodname"].push(v);
          });
          res.canhao.forEach(v => {
            this.form.canhaoarray[v.can - 3][`columns${v.foodtype}`] =
              v.quantity;
          });
          this.calcPerAverage();
          this.getThatDayStockBillCheckListApi();
        });
      });
    },
    // 新增单日伙食管理登记
    addDinnerRegisterApi() {
      return new Promise((resolve, reject) => {
        let canhaoarray = [];
        let foodarray = [];
        console.log('this.form.canhaoarray',this.form.canhaoarray);
        this.form.canhaoarray.forEach(v => {
          canhaoarray.push({ can: v.can, foodtype: 0, quantity: v.columns0 });
          canhaoarray.push({ can: v.can, foodtype: 1, quantity: v.columns1 });
          canhaoarray.push({ can: v.can, foodtype: 2, quantity: v.columns2 });
          canhaoarray.push({ can: v.can, foodtype: 3, quantity: v.columns3 });
          canhaoarray.push({ can: v.can, foodtype: 4, quantity: v.columns4 });
          canhaoarray.push({ can: v.can, foodtype: 5, quantity: v.columns5 });
          canhaoarray.push({ can: v.can, foodtype: 6, quantity: v.columns6 });
          canhaoarray.push({ can: v.can, foodtype: 7, quantity: v.columns7 });
          canhaoarray.push({ can: v.can, foodtype: 8, quantity: v.columns8 });
          canhaoarray.push({ can: v.can, foodtype: 9, quantity: v.columns9 });
          canhaoarray.push({ can: v.can, foodtype: 10, quantity: v.columns10 });
          canhaoarray.push({ can: v.can, foodtype: 11, quantity: v.columns11 });
          canhaoarray.push({ can: v.can, foodtype: 12, quantity: v.columns12 });
          canhaoarray.push({ can: v.can, foodtype: 13, quantity: v.columns13 });
          canhaoarray.push({ can: v.can, foodtype: 14, quantity: v.columns14 });
          canhaoarray.push({ can: v.can, foodtype: 15, quantity: v.columns15 });
          canhaoarray.push({ can: v.can, foodtype: 16, quantity: v.columns16 });
          canhaoarray.push({ can: v.can, foodtype: 17, quantity: v.columns17 });
          canhaoarray.push({ can: v.can, foodtype: 18, quantity: v.columns18 });
          canhaoarray.push({ can: v.can, foodtype: 19, quantity: v.columns19 });
        });
        console.log('this.form.foodarray',this.form.foodarray);
        this.form.foodarray.forEach(v => {
          v.foodname.forEach(val => {
            foodarray.push({
              can: v.can,
              foodname: val.dishName,
              foodcode: val.dishCode
            });
          });
        });

        addDinnerRegister({
          canhaoarray: JSON.stringify(canhaoarray),
          canhaosn: `canhao${new Date().Format("yyyyMMddhhmmss")}${Math.floor(
            Math.random() * 900
          ) + 100}`,
          certname: `${this.form.huoshidate.Format("yyyy年MM月dd")}伙食凭证`,
          department: this.departmentList.length
            ? this.departmentList[0].unitName
            : "",
          fankeb: 0,
          fankel: 0,
          fankes: 0,
          foodarray: JSON.stringify(foodarray),
          foodsn: `food${new Date().Format("yyyyMMddhhmmss")}${Math.floor(
            Math.random() * 900
          ) + 100}`,
          huoshidate: this.form.huoshidate.Format("yyyy-MM-dd"),
          lianduib: this.form.foodarray[0]["liandui"],
          lianduil: this.form.foodarray[1]["liandui"],
          lianduis: this.form.foodarray[2]["liandui"],
          mealPlanId: this.form.mealPlanId,
          mealPlanName: this.form.mealPlanName,
          remark: this.form.remark,
          rukuarray: JSON.stringify(this.form.rukuarray),
          userid: 0,
          wailaib: this.form.foodarray[0]["wailai"],
          wailail: this.form.foodarray[1]["wailai"],
          wailais: this.form.foodarray[2]["wailai"],
          weisheng: this.form.weisheng,
          wholeb: this.form.foodarray[0]["whole"],
          wholel: this.form.foodarray[1]["whole"],
          wholes: this.form.foodarray[2]["whole"],
          wuziarray: "[]",
          wuzisn: `wuzi${new Date().Format("yyyyMMddhhmmss")}${Math.floor(
            Math.random() * 900
          ) + 100}`,
          yaoshi: this.form.yaoshi,
          yijian: this.form.yijian,
          head: this.form.head,
          director: this.form.director,
          zhibanyuan: this.form.zhibanyuan
        }).then(res => {
          this.form.loading = false;
          if (res.errorcode === 0) {
            this.$Message.success("添加成功");
            this.$router.push({
              name: "单日伙食登记管理",
              query: {
                id: res.huoShiId
              }
            });
            this.huoshiid = res.huoShiId;
            this.form = this.$cu.deepClone(form);
          }
        });
      });
    }
  }
};
</script>

<style scoped lang="scss">
.dinnerRegister {
  position: relative;
  height: 100%;
  overflow: auto;
}
.button-textarea-wrap {
  display: flex;
  justify-content: space-between;
  padding: 0px 24px;
  .ivu-form-item {
    width: 30%;
    ::v-deep .ivu-input {
      height: auto !important;
    }
  }
}
.form-item-disabled-wrap {
  display: flex;
  align-items: center;
  height: 40px;
}
</style>
